package com.habdqn.filght.information.web;

import com.habdqn.filght.information.entity.Flight;
import com.habdqn.filght.information.service.FlightService;
import com.habdqn.filght.information.service.impl.FlightServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Xing.Jiejian
 * @date 2020/11/23
 */
@WebServlet(name = "SearchFlightServlet",urlPatterns = "/flight/search")
public class SearchFlightServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date date = null;
        String strDate = request.getParameter("date");
        if (strDate!=null && !"".equals(strDate)){
            try {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(strDate.trim());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String takeCity = request.getParameter("takeCity");
        String landingCity = request.getParameter("landingCity");

        FlightService flightService = new FlightServiceImpl();
        List<Flight> flightList = flightService.find(date,takeCity,landingCity);

        request.setAttribute("flights",flightList);


        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }
}
